package com.example.administrator.szb.util;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.szb.R;

/**
 * Created by stone on 2018/1/22.
 */

public class IosDiaolog {
    //使用示例
    public void test(){
        //alert
        Activity test_ctx=null;
        View test_view=null;

        IosDiaolog iosDiaolog = new IosDiaolog(test_ctx);
        iosDiaolog.reset();
        iosDiaolog.setMsgs("确定删除？");
        iosDiaolog.setRightClick(new OnIosDialogClickListener() {
            @Override
            public void onClick(String str, PopupWindow popupWindows) {
                //执行操作
            }
        });
        iosDiaolog.show(test_view);
    }




    private PopupWindow popupWindow;
    private TextView tips;
    private LinearLayout edit_ll;
    private EditText editText;
    private TextView error_msg;
    private TextView leftBtn;
    private TextView rightBtn;
    
    private Activity context;
    private String msgs="提示";//提示内容
    private String confirmMsg="请输入内容";//编辑框提示内容

    private String errorMsg="";//错误内容
    private String leftButton="取消";//左边Button显示字
    private String rightButton="确定";//右边Button显示字
    private boolean atuoHide=true;//点击外侧是否自动消失
    private boolean atuoHideLeft = true;//点击左侧按钮是自动消失
    private boolean atuoHideRight = true;//点击右侧按钮是自动消失
    private OnIosDialogClickListener leftClick;
    private OnIosDialogClickListener rightClick;

    public void setRightClick(OnIosDialogClickListener rightClick) {
        this.rightClick = rightClick;
    }

    public void setLeftClick(OnIosDialogClickListener leftClick) {
        this.leftClick = leftClick;
    }

    public PopupWindow getPopupWindow() {
        return popupWindow;
    }

    public boolean isAtuoHide() {
        return atuoHide;
    }

    public IosDiaolog(Activity context) {
        this.context = context;
        popupWindow = new PopupWindow(bindBiew(),ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        initEvent();
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(atuoHide);// 点击空白处时，隐藏掉pop窗口

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
    }

    public void setMsgs(String msg) {
        this.msgs = msg;
        tips.setText(this.msgs);

    }
    public void setMsgs2(SpannableStringBuilder msg) {
        this.msgs = msg.toString();
        tips.setText(msg);
    }
    public TextView getTips() {
        return tips;
    }

    public void setConfirmMsg(String confirmMsg) {
        this.confirmMsg = confirmMsg;
        editText.setHint(this.confirmMsg);
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        error_msg.setText(this.errorMsg);
    }

    public void setLeftButton(String leftButton) {
        this.leftButton = leftButton;
        leftBtn.setText(this.leftButton);

    }

    public void setRightButton(String rightButton) {
        this.rightButton = rightButton;
        rightBtn.setText(this.rightButton);
    }

    public void setAtuoHide(boolean atuoHides) {
        this.atuoHide = atuoHides;
        popupWindow.setFocusable(this.atuoHide);
        popupWindow.setOutsideTouchable(atuoHide);

    }

    public void setAtuoHideLeft(boolean atuoHideLeft) {
        this.atuoHideLeft = atuoHideLeft;
    }

    public void setAtuoHideRight(boolean atuoHideRight) {
        this.atuoHideRight = atuoHideRight;
    }

    public void alert(View view){
        edit_ll.setVisibility(View.GONE);
        show(view);
    }
    public void confirm(View view){
        edit_ll.setVisibility(View.VISIBLE);
        show(view);
    }


    public void reset(){
        msgs = "提示";
        confirmMsg = "请输入内容";
        errorMsg = "";
        leftButton = "取消";
        rightButton = "确定";
        atuoHide = true;
        atuoHideLeft = true;
        atuoHideRight = true;

    }
    private View bindBiew(){
        View popView = LayoutInflater.from(context).inflate(
                R.layout.ios_dialog_layout, null);
        tips = (TextView) popView.findViewById(R.id.tips);
        edit_ll = (LinearLayout) popView.findViewById(R.id.edit_ll);
        editText = (EditText) popView.findViewById(R.id.editText);
        error_msg = (TextView) popView.findViewById(R.id.error_msg);
        leftBtn = (TextView) popView.findViewById(R.id.leftBtn);
        rightBtn = (TextView) popView.findViewById(R.id.rightBtn);
        return popView;
    }
    public void initEvent(){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                error_msg.setText("");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (atuoHideLeft){
                    hide();
                }
                if (leftClick!=null){
                    leftClick.onClick(editText.getText().toString().trim(),popupWindow);
                }
            }
        });
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (atuoHideRight){
                    hide();
                }
                if (rightClick!=null){
                    rightClick.onClick(editText.getText().toString().trim(),popupWindow);
                }
            }
        });
    }
    public void show(View view){
        if (popupWindow!=null){
            setBackgroundAlpha(0.5f);//设置屏幕透明度
            popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
        }

    }
    public void hide(){
        if (popupWindow!=null) {
            popupWindow.dismiss();
        }
    }
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = (context).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }

   public interface OnIosDialogClickListener{
        void onClick(String str, PopupWindow popupWindows);
    }
}
